package com.gz.mylife.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;

import com.gz.mylife.R;
import com.gz.mylife.util.EmojiResource;


/**
 * 
 * @author 陈希然
 *
 */
public class EmojiLayout extends LinearLayout implements OnClickListener, OnPageChangeListener {

	private static final int EMOJI_CATEGORY_BELL = 0;
	private static final int EMOJI_CATEGORY_CAR = 1;
	private static final int EMOJI_CATEGORY_FLOWER = 2;
	private static final int EMOJI_CATEGORY_NUMBER = 3;
	private static final int EMOJI_CATEGORY_SMILE = 4;
	
	private int mEmojiCategory;
	
	private OnEmojiClickListener mListener;
	
	private Context mContext;
	private Button mSmile;
	private Button mBell;
	private Button mFlower;
	private Button mCar;
	private Button mNumber;
	private Button mDelete;
	private ViewPager mPagerSmail;
	private ViewPager mPagerBell;
	private ViewPager mPagerFlower;
	private ViewPager mPagerCar;
	private ViewPager mPagerNumber;
	
	private EmojiPageIndicator mBellIndicator;
	private EmojiPageIndicator mCarIndicator;
	private EmojiPageIndicator mFlowerIndicator;
	private EmojiPageIndicator mNumberIndicator;
	private PageIndicator mSmileIndicator;
	
	public EmojiLayout(Context context) {
		super(context);
		mContext = context;
	}

	public EmojiLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		mBellIndicator = (EmojiPageIndicator) findViewById(R.id.bell_indicator);
		mCarIndicator = (EmojiPageIndicator) findViewById(R.id.car_indicator);
		mFlowerIndicator = (EmojiPageIndicator) findViewById(R.id.flower_indicator);
		mNumberIndicator = (EmojiPageIndicator) findViewById(R.id.number_indicator);
		mSmileIndicator = (PageIndicator) findViewById(R.id.smile_indicator);
		
		mSmile = (Button) findViewById(R.id.btn_smile);
		mSmile.setOnClickListener(this);
		mBell = (Button) findViewById(R.id.btn_bell);
		mBell.setOnClickListener(this);
		mFlower = (Button) findViewById(R.id.btn_flower);
		mFlower.setOnClickListener(this);
		mCar = (Button) findViewById(R.id.btn_car);
		mCar.setOnClickListener(this);
		mNumber = (Button) findViewById(R.id.btn_number);
		mNumber.setOnClickListener(this);
		mDelete = (Button) findViewById(R.id.btn_delete);
		mDelete.setOnClickListener(this);
		
		mPagerBell = (ViewPager) findViewById(R.id.pager_bell);
		mPagerCar = (ViewPager) findViewById(R.id.pager_car);
		mPagerFlower = (ViewPager) findViewById(R.id.pager_flower);
		mPagerNumber = (ViewPager) findViewById(R.id.pager_number);
		mPagerSmail = (ViewPager) findViewById(R.id.pager_smail);
		mPagerBell.setOnPageChangeListener(this);
		mPagerCar.setOnPageChangeListener(this);
		mPagerFlower.setOnPageChangeListener(this);
		mPagerNumber.setOnPageChangeListener(this);
		mPagerSmail.setOnPageChangeListener(this);
		
		initEmoji();
	}
	
	private void initEmoji() {
		List<View> tempSmileView = new ArrayList<View>();
		int[] tempEmojis = null;
		for (int i = 0; i < EmojiResource.PAGE_SMILE_COUNT; i++) {
			View ll = LayoutInflater.from(mContext).inflate(
					R.layout.emoji_page_view, null);
			GridView gridview = (GridView) ll.findViewById(R.id.emoji_gd);
			switch (i) {
			case 0:
				tempEmojis = EmojiResource.smile_1;
				break;
			case 1:
				tempEmojis = EmojiResource.smile_2;
				break;
			case 2:
				tempEmojis = EmojiResource.smile_3;
				break;
			case 3:
				tempEmojis = EmojiResource.smile_4;
				break;
			case 4:
				tempEmojis = EmojiResource.smile_5;
				break;
			case 5:
				tempEmojis = EmojiResource.smile_6;
				break;
			case 6:
				tempEmojis = EmojiResource.smile_7;
				break;
			case 7:
				tempEmojis = EmojiResource.smile_8;
				break;
			case 8:
				tempEmojis = EmojiResource.smile_9;
				break;
			}

			// 添加每页数据
			ArrayList<HashMap<String, Object>> smileEmojiList = new ArrayList<HashMap<String, Object>>();
			for (int j = 0; j < tempEmojis.length; j++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("emoji", tempEmojis[j]);
				smileEmojiList.add(map);
			}
			SimpleAdapter emojiAdapter = new SimpleAdapter(mContext,
					smileEmojiList, R.layout.emoji_item,
					new String[] { "emoji" }, new int[] { R.id.image_iv });
			gridview.setAdapter(emojiAdapter);
			final int[] smileEmoji = tempEmojis;
			gridview.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if(mListener != null){
						mListener.onEmojiClick(smileEmoji[position]);
					}
				}
			});
			tempSmileView.add(ll);
		}
		mPagerSmail.setAdapter(new EmojiAdapter(tempSmileView));
	}
	
	public void setOnEmojiClickListener(OnEmojiClickListener listener){
		mListener = listener;
	}

	public Button getSmile() {
		return mSmile;
	}

	public Button getBell() {
		return mBell;
	}

	public Button getFlower() {
		return mFlower;
	}

	public Button getCar() {
		return mCar;
	}

	public Button getNumber() {
		return mNumber;
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_delete:
			if(mListener != null){
				mListener.onEmojiDeleteClick();
			}
			break;
		
		case R.id.btn_smile:
			mPagerBell.setVisibility(View.GONE);
			mPagerCar.setVisibility(View.GONE);
			mPagerFlower.setVisibility(View.GONE);
			mPagerNumber.setVisibility(View.GONE);
			mPagerSmail.setVisibility(View.VISIBLE);
			
//			mBell.setBackgroundResource(R.drawable.bells_normal);
//			mCar.setBackgroundResource(R.drawable.cars_normal);
//			mFlower.setBackgroundResource(R.drawable.flowers_normal);
//			mNumber.setBackgroundResource(R.drawable.number_normal);
			mSmile.setBackgroundResource(R.drawable.smail_select);
			
			mPagerSmail.setCurrentItem(0, true);
//			mSmileIndicator.drawPageIndicator(0, EmojiResource.PAGE_SMILE_COUNT);
			mSmileIndicator.setViewPage(mPagerSmail);
			mSmileIndicator.notifyDataSetChanged();
			mSmileIndicator.setVisibility(View.VISIBLE);
			mBellIndicator.setVisibility(View.GONE);
			mFlowerIndicator.setVisibility(View.GONE);
			mNumberIndicator.setVisibility(View.GONE);
			mCarIndicator.setVisibility(View.GONE);
			
			mEmojiCategory = EMOJI_CATEGORY_SMILE;
			
			break;
			
		case R.id.btn_bell:
			mPagerBell.setVisibility(View.VISIBLE);
			mPagerCar.setVisibility(View.GONE);
			mPagerFlower.setVisibility(View.GONE);
			mPagerNumber.setVisibility(View.GONE);
			mPagerSmail.setVisibility(View.GONE);

//			mBell.setBackgroundResource(R.drawable.bells_select);
//			mCar.setBackgroundResource(R.drawable.cars_normal);
//			mFlower.setBackgroundResource(R.drawable.flowers_normal);
//			mNumber.setBackgroundResource(R.drawable.number_normal);
			mSmile.setBackgroundResource(R.drawable.smail_normal);

			mPagerBell.setCurrentItem(0);
			mBellIndicator.drawPageIndicator(0, EmojiResource.PAGE_BELL_COUNT);
			mSmileIndicator.setVisibility(View.GONE);
			mBellIndicator.setVisibility(View.VISIBLE);
			mFlowerIndicator.setVisibility(View.GONE);
			mNumberIndicator.setVisibility(View.GONE);
			mCarIndicator.setVisibility(View.GONE);

			mEmojiCategory = EMOJI_CATEGORY_BELL;
			
			break;
			
		case R.id.btn_flower:
			mPagerBell.setVisibility(View.GONE);
			mPagerCar.setVisibility(View.GONE);
			mPagerFlower.setVisibility(View.VISIBLE);
			mPagerNumber.setVisibility(View.GONE);
			mPagerSmail.setVisibility(View.GONE);

//			mBell.setBackgroundResource(R.drawable.bells_normal);
//			mCar.setBackgroundResource(R.drawable.cars_normal);
//			mFlower.setBackgroundResource(R.drawable.flowers_select);
//			mNumber.setBackgroundResource(R.drawable.number_normal);
			mSmile.setBackgroundResource(R.drawable.smail_normal);

			mPagerFlower.setCurrentItem(0);
			mFlowerIndicator.drawPageIndicator(0, EmojiResource.PAGE_FLOWER_COUNT);
			mSmileIndicator.setVisibility(View.GONE);
			mBellIndicator.setVisibility(View.GONE);
			mFlowerIndicator.setVisibility(View.VISIBLE);
			mNumberIndicator.setVisibility(View.GONE);
			mCarIndicator.setVisibility(View.GONE);

			mEmojiCategory = EMOJI_CATEGORY_FLOWER;
			
			break;
			
		case R.id.btn_car:
			mPagerBell.setVisibility(View.GONE);
			mPagerCar.setVisibility(View.VISIBLE);
			mPagerFlower.setVisibility(View.GONE);
			mPagerNumber.setVisibility(View.GONE);
			mPagerSmail.setVisibility(View.GONE);

//			mBell.setBackgroundResource(R.drawable.bells_normal);
//			mCar.setBackgroundResource(R.drawable.cars_select);
//			mFlower.setBackgroundResource(R.drawable.flowers_normal);
//			mNumber.setBackgroundResource(R.drawable.number_normal);
			mSmile.setBackgroundResource(R.drawable.smail_normal);

			mPagerCar.setCurrentItem(0);
			mCarIndicator.drawPageIndicator(0, EmojiResource.PAGE_CAR_COUNT);
			mSmileIndicator.setVisibility(View.GONE);
			mBellIndicator.setVisibility(View.GONE);
			mFlowerIndicator.setVisibility(View.GONE);
			mNumberIndicator.setVisibility(View.GONE);
			mCarIndicator.setVisibility(View.VISIBLE);

			mEmojiCategory = EMOJI_CATEGORY_CAR;
			
			break;
			
		case R.id.btn_number:
			mPagerBell.setVisibility(View.GONE);
			mPagerCar.setVisibility(View.GONE);
			mPagerFlower.setVisibility(View.GONE);
			mPagerNumber.setVisibility(View.VISIBLE);
			mPagerSmail.setVisibility(View.GONE);

//			mBell.setBackgroundResource(R.drawable.bells_normal);
//			mCar.setBackgroundResource(R.drawable.cars_normal);
//			mFlower.setBackgroundResource(R.drawable.flowers_normal);
//			mNumber.setBackgroundResource(R.drawable.number_select);
			mSmile.setBackgroundResource(R.drawable.smail_normal);

			mPagerNumber.setCurrentItem(0);
			mNumberIndicator.drawPageIndicator(0, EmojiResource.PAGE_NUMBER_COUNT);
			mSmileIndicator.setVisibility(View.GONE);
			mBellIndicator.setVisibility(View.GONE);
			mFlowerIndicator.setVisibility(View.GONE);
			mNumberIndicator.setVisibility(View.VISIBLE);
			mCarIndicator.setVisibility(View.GONE);

			mEmojiCategory = EMOJI_CATEGORY_NUMBER;
			
			break;
		}
	}
	
	private class EmojiAdapter extends PagerAdapter {

		// 界面列表
		private List<View> mPagers;

		public EmojiAdapter(List<View> views) {
			this.mPagers = views;
		}

		// 销毁arg1位置的界面
		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mPagers.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {

		}

		// 获得当前界面数
		@Override
		public int getCount() {
			if (mPagers != null) {
				return mPagers.size();
			}

			return 0;
		}

		// 初始化arg1位置的界面
		@Override
		public Object instantiateItem(View arg0, int arg1) {

			((ViewPager) arg0).addView(mPagers.get(arg1), 0);

			return mPagers.get(arg1);
		}

		// 判断是否由对象生成界面
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return (arg0 == arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

	}

	@Override
	public void onPageSelected(int position) {
		switch (mEmojiCategory) {
		case EMOJI_CATEGORY_BELL:
			mBellIndicator.drawPageIndicator(position, EmojiResource.PAGE_BELL_COUNT);
			break;

		case EMOJI_CATEGORY_CAR:
			mCarIndicator.drawPageIndicator(position, EmojiResource.PAGE_CAR_COUNT);
			break;

		case EMOJI_CATEGORY_FLOWER:
			mFlowerIndicator.drawPageIndicator(position, EmojiResource.PAGE_FLOWER_COUNT);
			break;

		case EMOJI_CATEGORY_NUMBER:
			mNumberIndicator.drawPageIndicator(position, EmojiResource.PAGE_NUMBER_COUNT);
			break;

		case EMOJI_CATEGORY_SMILE:
//			mSmileIndicator.drawPageIndicator(position, EmojiResource.PAGE_SMILE_COUNT);
			mSmileIndicator.setViewPage(mPagerSmail);
			mSmileIndicator.notifyDataSetChanged();
			break;

		}
	}

	@Override
	public void onPageScrollStateChanged(int state) {}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {	
	}
	
	public static interface OnEmojiClickListener {
		void onEmojiClick(int emojiResourceId);
		void onEmojiDeleteClick();
	}
}
